package com.example.permissionservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.permissionservice.entity.UserRole;
import com.example.permissionservice.mapper.UserRoleMapper;
import com.example.permissionservice.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Override
    public void bindDefaultRole(Long userId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(2);
        if(!save(userRole)){
            throw new RuntimeException("用户绑定角色失败");
        }
    }

    @Override
    public String getUserRoleCode(Long userId) {
        return baseMapper.selectUserRoleByUserId(userId);
    }

    @Override
    public void upgradeToAdmin(Long userId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(1);
        baseMapper.upgradeByUserId(userId);
    }

    @Override
    public void downgradeToUser(Long userId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(2);
        baseMapper.downgradeByUserId(userId);
    }

    @Override
    public List<Long> getUsers() {
        return baseMapper.selectUsers();
    }
}
